<?php

namespace Admin\Controller;

use Think\AjaxPage;
use Think\Page;


class DoctorController extends BaseController {
	 public function index(){
       $res = $list = array();
        $keywords = I('keywords');
        $is_admin=session();
        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        $roleid=$roleid['role_id'];
    	if(empty($keywords)){
           
    		$res = D('seller')->field('ty_users.nickname,ty_seller.*')->join('ty_users on ty_seller.user_id= ty_users.user_id')->where(" ty_seller.group_id=8 and store_id=0 and is_admin=0  and ty_users.roleid=$roleid")->select();
    	}else{

    		$res = D()->query("select * from __PREFIX__seller where  seller_name like '%$keywords%' order by seller_id");
    	}
    	$group = D('seller_group')->getField('group_id,group_name');

    	if($res && $group){
    		foreach ($res as $val){
    			$val['role'] =  $group[$val['group_id']];
    			$val['enabled'] = $val['enabled']>0 ? '启用' : '停用';
    			$val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
    			$list[] = $val;
    		}
    	}
        //var_dump($list);
    	$this->assign('list',$list);
        $this->display();
    }
    public function admin_info(){
    	$seller_id = I('get.seller_id');
        $is_admin=session('seller');
        $is_admin=$is_admin['is_admin'];

    	if($seller_id>0){
    		$info = D('seller')->where("seller_id=$seller_id")->find(); 
            $shangji_id=$info['shangji_id'];

            $store_id=$info['store_id'];
            $group_id = D('seller')->where("seller_id=$shangji_id")->find();
           if($group_id==''){
               $group_id='0';
            $sql="select b.nickname ,a.seller_id from ty_seller a, ty_users b  where  a.store_id=$store_id and a.user_id=b.user_id";
            $Model = M();
            $list = $Model->query($sql);
            $this->assign('list',$list);
           }else{
                $group_id=$group_id['group_id'];
            $sql="select b.nickname ,a.seller_id from ty_seller a, ty_users b  where a.group_id=$group_id and a.store_id=$store_id and a.user_id=b.user_id";
            $Model = M();
            $list = $Model->query($sql);
            $this->assign('list',$list);

           }
           
            
    		if($info){
    			$user = M('users')->where("user_id=".$info['user_id'])->find();

    		}
    		$info['user_name'] = empty($user['mobile']) ? $user['email'] : $user['mobile'];
    		$this->assign('info',$info);
    	}
        //$res = D('seller')->field('ty_users.nickname,ty_seller.seller_id')->join('ty_users on ty_seller.user_id= ty_users.user_id')->where("store_id=".STORE_ID)->select();
        $role = D('seller_group')->where("store_id =0")->select();
    	$this->assign('role',$role);
    	//$this->assign('res',$res);
        $this->assign('is_admin',$is_admin);
        $this->display();
    }
    public function adminHandle(){
    	$data = I('post.');

    	$is_admin=session();
        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();

    		//验证商家后台登陆账号是否有同名	
    		if(M('seller')->where("seller_name='".$data['seller_name']."'")->count()){
    			$this->error("此登陆账号名已被注册，请更换",U('Admin/admin_info'));
    		}
    		$uname = check_email($data['user_name']) ? 'email' : 'mobile';

    		//查找验证绑定用户
    		$userinfo = M('users')->where("$uname ='".$data['user_name']."'")->find();
    		if(empty($userinfo)){
    			$this->error("请先注册前台会员",U('Admin/admin_info'));
    		}elseif($userinfo['password'] != encrypt($data['password'])){
    			$this->error("登陆密码错误",U('Admin/admin_info'));
    		}else{
    			if(M('seller')->where("user_id=".$userinfo['user_id'])->count()){
    				$this->error("该用户已经添加过店铺管理员",U('Admin/admin_info'));
    			}
    			$data['password'] = encrypt($data['password']);
    			$data['user_id'] = $userinfo['user_id'];
    			$data['store_id'] = STORE_ID;
    			$data['add_time'] = time();
                	$data['roleid'] = $roleid['role_id'];
    			unset($data['seller_id']);
    			$r = M('seller')->add($data);
                $a= M('users')->where(array('user_id'=>
                    $data['user_id']))->save(array('group_id'=>$data['group_id']));
    		}
    	
    	if($r){
    		$this->success("操作成功",U('Doctor/index'));
    	}else{
    		$this->error("操作失败",U('Doctor/index'));
    	}
    }
    

}
?>